package org.linlinjava.litemall.admin.web2.system;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req10;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req11;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req12;
import org.linlinjava.litemall.core.util.R;
import org.linlinjava.litemall.db.dao.TFreightTemplateMapper;
import org.linlinjava.litemall.db.dao.TTimelyFreightTemplateMapper;
import org.linlinjava.litemall.db.domain.TFreightTemplate;
import org.linlinjava.litemall.db.domain.TFreightTemplateExample;
import org.linlinjava.litemall.db.domain.TTimelyFreightTemplate;
import org.linlinjava.litemall.db.domain.TTimelyFreightTemplateExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统设置
 */
@Api("系统设置-配送模板管理")
@RestController
@RequestMapping("/system/send")
public class SystemSendController {


    @Autowired
    private TFreightTemplateMapper freightTemplateMapper;

    @ApiOperation(value = "普通-配送运费模板list")
    @GetMapping("/normalList")
    public Object normalList(@RequestParam Integer page, @RequestParam Integer size) {
        PageHelper.startPage(page, size);
        List<TFreightTemplate> list = freightTemplateMapper.selectByExample(TFreightTemplateExample.newAndCreateCriteria().example());
        return R.okList(list);
    }

    @ApiOperation(value = "普通-新增或修改配送运费模板")
    @PostMapping("/normalSave")
    public Object normalSave(@RequestBody Req10 req) {
        Integer id = req.getId();
        TFreightTemplate entity;
        if (id != null) {
            entity = freightTemplateMapper.selectByPrimaryKey(id);
            entity.setUpdateTime(LocalDateTime.now());
            entity.setPrice(new BigDecimal(req.getPrice()));
            freightTemplateMapper.updateByPrimaryKey(entity);
        } else {
            BigDecimal sendDistance = new BigDecimal(req.getSendDistance());
            TFreightTemplate old = freightTemplateMapper.selectOneByExample(TFreightTemplateExample.newAndCreateCriteria().andSendDistanceEqualTo(sendDistance).example());
            if (old != null) {
                return R.fail("配送范围和送货距离已经存在，不能重复！");
            }
            entity = new TFreightTemplate();
            entity.setSendDistance(sendDistance);
            entity.setCreatedTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            entity.setPrice(new BigDecimal(req.getPrice()));
            freightTemplateMapper.insert(entity);
        }
        return R.ok();
    }

    @Autowired
    private TTimelyFreightTemplateMapper tTimelyFreightTemplateMapper;

    @ApiOperation(value = "及时达-配送运费模板list")
    @GetMapping("/timelyList")
    public Object timelyList(@RequestParam Integer page, @RequestParam Integer size) {
        PageHelper.startPage(page, size);
        List<TTimelyFreightTemplate> list = tTimelyFreightTemplateMapper.selectByExample(TTimelyFreightTemplateExample.newAndCreateCriteria().example());
        return R.okList(list);
    }

    @ApiOperation(value = "及时达-新增或修改配送运费模板")
    @PostMapping("/timelySave")
    public Object timelySave(@RequestBody Req11 req) {
        Integer id = req.getId();
        TTimelyFreightTemplate entity;
        if (id != null) {
            entity = tTimelyFreightTemplateMapper.selectByPrimaryKey(id);
            entity.setUpdateTime(LocalDateTime.now());
            entity.setPrice(new BigDecimal(req.getPrice()));
            entity.setFreeAmount(new BigDecimal(req.getFreeAmount()));
            tTimelyFreightTemplateMapper.updateByPrimaryKey(entity);
        } else {
            BigDecimal sendDistance = new BigDecimal(req.getSendDistance());
            TTimelyFreightTemplate old = tTimelyFreightTemplateMapper.selectOneByExample(TTimelyFreightTemplateExample.newAndCreateCriteria().andSendDistanceEqualTo(sendDistance).example());
            if (old != null) {
                return R.fail("送货距离已经存在，不能重复！");
            }
            entity = new TTimelyFreightTemplate();
            entity.setSendDistance(new BigDecimal(req.getSendDistance()));
            entity.setCreatedTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            entity.setPrice(new BigDecimal(req.getPrice()));
            entity.setFreeAmount(new BigDecimal(req.getFreeAmount()));
            tTimelyFreightTemplateMapper.insert(entity);
        }
        return R.ok();
    }

    @ApiOperation(value = "普通-删除运费模板")
    @PostMapping("/normalDel")
    public Object normalDel(@RequestBody Req12 req) {
        int i = freightTemplateMapper.deleteByPrimaryKey(req.getId());
        return R.ok(i);
    }

    @ApiOperation(value = "及时达-删除运费模板")
    @PostMapping("/timelyDel")
    public Object timelyDel(@RequestBody Req12 req) {
        int i = tTimelyFreightTemplateMapper.deleteByPrimaryKey(req.getId());
        return R.ok(i);
    }
}
